// pages/appointment/index.ts
const app = getApp<IAppOption>()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: '',
        mobile: '',
        address_home: '',
        address_school: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1
            })
        }
    },
    bindUserNameInput: function (e: any) {
        this.setData({
            username: e.detail.value
        })
    },
    bindMobileInput: function (e: any) {
        this.setData({
            username: e.detail.value
        })
    },
    bindAddressHomeInput: function (e: any) {
        this.setData({
            username: e.detail.value
        })
    },
    bindAddressSchoolInput: function (e: any) {
        this.setData({
            username: e.detail.value
        })
    },
    formSubmit: function (e: any) {
        let that = this
        console.log('form发生了submit事件，携带数据为：', e.detail.value.username)
        if (!e.detail.value.username) {
            wx.showToast({
                title: '请输入姓名',
                icon: 'error',
                duration: 2000
            })
            return
        }
        if (!e.detail.value.mobile) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'error',
                duration: 2000
            })
            return
        }
        let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (!myreg.test(e.detail.value.mobile) || e.detail.value.mobile.length !== 11) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'error',
                duration: 2000
            })
            return
        }
        if (!e.detail.value.address_home) {
            wx.showToast({
                title: '请输入家庭住址',
                icon: 'error',
                duration: 2000
            })
            return
        }
        wx.request({
            method: 'POST',
            url: app.globalData.apiUrl + 'api/appointment/create',
            data: {
                username: e.detail.value.username,
                mobile: e.detail.value.mobile,
                address_home: e.detail.value.address_home,
                address_school: e.detail.value.address_school
            },
            success: function (res: any) {
                if (res.data.code == 0) {
                    wx.showToast({
                        title: '预约成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.setData({
                        username: '',
                        mobile: '',
                        address_home: '',
                        address_school: ''
                    })
                }
            }
        })

    },
    formReset: function () {
        console.log('form发生了reset事件')
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
export default app